package com.ruoyi.web.controller.course;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.ruoyi.course.domain.CourseStudyRecord;
import com.ruoyi.course.domain.CourseWare;
import com.ruoyi.course.service.ICourseStudyRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.course.domain.CourseTrainSection;
import com.ruoyi.course.service.ICourseTrainSectionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 培训课程章节表Controller
 *
 * @author zzx
 * @date 2023-03-29
 */
@Api("培训课程章节")
@RestController
@RequestMapping("/course/course_train_section")
public class CourseTrainSectionController extends BaseController {
    @Autowired private ICourseTrainSectionService courseTrainSectionService;
    @Autowired private ICourseStudyRecordService courseStudyRecordService;


    /**
     * 查询培训课程章节表列表
     */
    @GetMapping("/list")
    @ApiOperation("查询培训课程章节表列表")
    public TableDataInfo list(CourseTrainSection courseTrainSection) {
        startPage();
        List<CourseTrainSection> list = courseTrainSectionService.selectCourseTrainSectionList(courseTrainSection);
        //获取学习记录
        CourseStudyRecord courseStudyRecord = null ;
        try{
            //查询当前用户的学习记录
            CourseStudyRecord courseStudy = CourseStudyRecord.builder().trainId(courseTrainSection.getTrainId()).userId(getUserId()).build();
            List<CourseStudyRecord> courseStudyRecords = courseStudyRecordService.selectCourseStudyRecordList(courseStudy);
            if (ObjectUtil.isNotEmpty(courseStudyRecords)){
                courseStudyRecord = courseStudyRecords.get(0);
                String studyList;
                if (ObjectUtil.isNotEmpty(courseStudyRecord) && StrUtil.isNotBlank(studyList = courseStudyRecord.getStudyList())){
                    List<Long> studyIds = JSONUtil.toList(studyList, Long.class);
                    //遍历章节列表
                    for (CourseTrainSection section : list) {
                        try{
                            //遍历课件列表
                            for (CourseWare ware : section.getCourseWareList()) {
                                try{
                                    //如果存在，说明学习过，设置为true
                                    ware.setWatch(studyIds.contains(ware.getId()));
                                }catch (Exception e){
                                    logger.error("",e);
                                }
                            }
                        }catch (Exception e){
                            logger.error("",e);
                        }
                    }
                }
            }
        }catch (Exception e){
            logger.error("",e);
        }
        return getDataTable(list).setData(courseStudyRecord);
    }

    /**
     * 导出培训课程章节表列表
     */
    @Log(title = "培训课程章节表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CourseTrainSection courseTrainSection) {
        List<CourseTrainSection> list = courseTrainSectionService.selectCourseTrainSectionList(courseTrainSection);
        ExcelUtil<CourseTrainSection> util = new ExcelUtil<CourseTrainSection>(CourseTrainSection.class);
        util.exportExcel(response, list, "培训课程章节表数据");
    }

    /**
     * 获取培训课程章节表详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取培训课程章节表详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(courseTrainSectionService.selectCourseTrainSectionById(id));
    }

    /**
     * 新增培训课程章节表
     */
    @Log(title = "培训课程章节表", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增培训课程章节表")
    public AjaxResult add(@RequestBody CourseTrainSection courseTrainSection) {
        return toAjax(courseTrainSectionService.insertCourseTrainSection(courseTrainSection));
    }

    /**
     * 修改培训课程章节表
     */
    @Log(title = "培训课程章节表", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改培训课程章节表")
    public AjaxResult edit(@RequestBody CourseTrainSection courseTrainSection) {
        return toAjax(courseTrainSectionService.updateCourseTrainSection(courseTrainSection));
    }

    /**
     * 删除培训课程章节表
     */
    @Log(title = "培训课程章节表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除培训课程章节表")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(courseTrainSectionService.deleteCourseTrainSectionByIds(ids));
    }
}
